<?php
namespace app\configs\admin;

use app\admin\controller\Admin;
use think\Db;

use app\common\builder\ZBuilder; // 引入ZBuilder

class Postcard extends Admin
{
	

	//卡片列表
    public function index(){
		
        $map = $this->getMap();
        
        $data_list = Db::name('postcard')
        ->alias("a")
        ->join("category b","a.categoryId=b.categoryId",'left')
        ->where($map)
        ->field("a.*,b.name")
        ->order("postcardId desc")
        ->paginate();
        
        // 分页数据
        $page = $data_list->render();
        $category=db('category')->column("categoryId,name");
        
//         $btn_add = [
//             'title' => '添加文字',
//             'icon'  => 'fa fa-fw fa-plus-circle',
//             'href'  => url('add', ['id' => '__id__'])
//         ];
        
        $btn_album_view = [
            'title' => '查看体会',
            'icon'  => 'fa fa-fw fa-th',
            'href'  => url('album_list',['postcardId'=>'__postcardId__']),
        ];
        
        $btn_langdu_view = [
            'title' => '朗读列表',
            'icon'  => 'fa fa-fw fa-volume-up',
            'href'  => url('langdu_list', ['postcardId' => '__postcardId__'])
        ];
        
        $btn_copywrite_view = [
            'title' => '查看文案',
            'icon'  => 'fa fa-fw fa-paint-brush',
            'href'  => url('copywrite_list', ['postcardId' => '__postcardId__'])
        ];
        // 定义新增页面的字段
        $fields = [
            ['text', 'title', '名称', '必填'],
            ['text', 'minue', '(时长)分钟'],
            ['select', 'categoryId', '类型', '',$category,1],
            ['image', 'coverUrl', '封面图'],
            
            ['file','fileUrl','金句录音文件'],
            ['wangeditor', 'content', '文字内容'],
            ['hidden', 'create_time',date('Y-m-d H:i:s')],
            ['hidden', 'creat_date',date('Y-m-d')],
        ];
        
        $fields1 = [
            ['hidden', 'postcardId'],
            ['text', 'title', '名称', '必填'],
            ['text', 'minue', '(时长)分钟'],
            ['select', 'categoryId', '类型', '',$category],
            ['image', 'coverUrl', '封面图'],
            ['file','fileUrl','金句录音文件'],
            ['wangeditor', 'content', '文字内容'],
        ];
        
        
        $css = <<<EOF
           <style>
                .table-builder > tbody > tr > td{white-space: normal;}
           </style>
EOF;
        //		dump($data_list);die;
        
        return ZBuilder::make('table')
        ->setPageTitle('招聘列表')
        ->setTableName('postcard') // 指定数据表名
        ->setPrimaryKey('postcardId')	//设置主键
        ->hideCheckbox()
        ->setSearch(['title' => '名称']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['title', '名称','text.edit'],
            ['minue', '时长(分钟)','text.edit'],
            ['categoryId', '所属分类','select',$category],
            ['coverUrl', '图片','picture'],
            ['fileUrl', '录音文件','files'],
            ['content', '文字内容','popover'],
        ])
        ->addColumn('right_button', '操作', 'btn')
        ->autoAdd($fields,'postcard','',time())
        ->autoEdit($fields1,'postcard')
        ->addRightButton('btn_album_view',$btn_album_view) // 批量添加右侧按钮1
        ->addRightButton('btn_langdu_view',$btn_langdu_view) // 批量添加右侧按钮1
        ->addRightButton('delete') // 批量添加右侧按钮1
        //->addRightButton('btn_copywrite_view',$btn_copywrite_view) // 批量添加右侧按钮1
        ->setRowList($data_list) // 设置表格数据
        ->setPages($page) // 设置分页数据
        ->setExtraCss($css)
        ->fetch();
    }
	
	//体会列表
    public function album_list($postcardId){
        if(empty($postcardId)){
            return $this->error("请选择金句");
        }
        $data = Db::name('album')
        ->alias("a")
        ->join("user b","a.user_id=b.id","left")
        ->join("group c","a.group_id=c.id","left")
        ->field("a.*,b.username,b.nickname,c.name group_name")
        ->where("a.postcardId",$postcardId)
        ->order("a.albumId desc")
        ->paginate();
        $btn_add = [
            'title' => '添加相册',
            'icon'  => 'fa fa-fw fa-plus-circle',
            'href'  => url("album_add",['postcardId' => $postcardId])
        ];
        $btn_update = [
            'title' => '修改相册',
            'icon'  => 'fa fa-pencil fa-fw',
            'href'  => url("album_update",['albumId' => "__albumId__",'postcardId' => $postcardId])
        ];
        $btn_sound_view = [
            'title' => '阅读人员列表',
            'icon'  => 'fa fa-fw fa-user',
            'href'  => url('sound_list', ['albumId' => '__albumId__'])
        ];
        
        
        $btn_appraise_view = [
            'title' => '评价列表',
            'icon'  => 'fa fa-fw fa-file-o',
            'href'  => url('appraise_list', ['albumId' => '__albumId__'])
        ];
        
        
        
        
        
        
        return ZBuilder::make('table')
        ->setPageTitle('体会列表')
        ->setPrimaryKey('albumId')	//设置主键
        ->setTableName('album')
        ->hideCheckbox(true)
        ->setSearch(['username' => '发布人', 'content' => '文字内容']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['username', '姓名'],
            ['group_name', '所在组'],
            ['coverPath', '封面图','picture'],
            ['fileUrl', '录音文件'],
            //['title', '标题'],
            ['content', '内容'],
            ['createAt', '发布时间'],
            ['right_button', '操作', 'btn']
        ])
        
        ->setRowList($data) // 设置表格数据
       // ->addTopButton('btn_add', $btn_add) // 添加顶部按钮
        ->addRightButton('btn_sound_view',$btn_sound_view,1) // 批量添加右侧按钮1
        ->addRightButton('btn_appraise_view',$btn_appraise_view,1) // 批量添加右侧按钮1
        ->addRightButtons('delete') // 批量添加右侧按钮
       // ->addRightButton('btn_update', $btn_update) // 批量添加右侧按钮
        
        ->fetch();
    }
    
    
    //体会列表
    public function langdu_list($postcardId){
        if(empty($postcardId)){
            return $this->error("请选择金句");
        }
        $data = Db::name('langdu')
        ->alias("a")
        ->join("user b","a.user_id=b.id","left")
        ->join("group c","a.group_id=c.id","left")
        ->field("a.*,b.username,b.nickname,c.name group_name")
        ->where("a.postcardId",$postcardId)
        ->order("a.id desc")
        ->select();
        
        return ZBuilder::make('table')
        ->setPageTitle('朗读列表')
        ->setPrimaryKey('id')	//设置主键
        ->setTableName('langdu')
        ->hideCheckbox(true)
        ->setSearch(['username' => '朗读人']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['username', '朗读人'],
            ['group_name', '所在组'],
            ['coverPath', '封面图','picture'],
            ['fileUrl', '录音文件','callback',function($item){
                $str='';
                return  "<a href='".$item."' target='__blank'>试听</a>";
            }],
            //['title', '标题'],
           // ['content', '内容'],
            ['createAt', '发布时间'],
            ['right_button', '操作', 'btn']
        ])
        
        ->setRowList($data) // 设置表格数据
        // ->addTopButton('btn_add', $btn_add) // 添加顶部按钮
//         ->addRightButton('btn_sound_view',$btn_sound_view,1) // 批量添加右侧按钮1
//         ->addRightButton('btn_appraise_view',$btn_appraise_view,1) // 批量添加右侧按钮1
        ->addRightButtons('delete') // 批量添加右侧按钮
        // ->addRightButton('btn_update', $btn_update) // 批量添加右侧按钮
        
        ->fetch();
    }
    
    
    public function album_add($postcardId=null){
        if($postcardId==null){
            return $this->error("ID错误，请从列表中进入！");
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['createAt']=date("Y-m-d H:i:s");
            //halt($data);
            $r=db('album')->insert($data);
            if($r){
                return $this->success('添加成功',url('/configs/postcard/album_list/postcardId/'.$postcardId));
            }else {
                return $this->error('添加失败',url('/configs/postcard/album_list/postcardId/'.$postcardId));
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
        ->setPageTitle('新增') // 设置页面标题
        ->addFormItems([ // 批量添加表单项
            ['hidden', 'postcardId',$postcardId],
            ['text', 'anchorName', '作者'],
            ['text', 'title', '标题', ''],
            ['text', 'description', '描述'],
            ['image', 'coverPath', '图片'],
        ])
        //->isAjax(false)
        ->fetch();
    }
    
    
    public function album_update($albumId=null,$postcardId=1){
        if ($albumId === null) $this->error('缺少参数');
        $info = db('album')->find($albumId);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $r=db('album')->where("albumId",$albumId)->update($data);
            if($r){
                return $this->success('修改成功',url('/configs/postcard/album_list/postcardId/'.$postcardId));
            }else {
                return $this->error('修改失败',url('/configs/postcard/album_list/postcardId/'.$postcardId));
            }
        }
        return ZBuilder::make('form')
        ->setPageTitle('编辑') // 设置页面标题
        ->addFormItems([ // 批量添加表单项
            ['text', 'anchorName', '作者'],
            ['text', 'title', '标题', ''],
            ['text', 'description', '描述'],
            ['image', 'coverPath', '图片'],
        ])
        ->setFormData($info) // 设置表单数据
        ->fetch();
        
    }
    
    
    public function copywrite_list($postcardId){
        if(empty($postcardId)){
            return $this->error("请选择卡片");
        }
        $data = Db::name('copywrite')
        ->where("postcardId",$postcardId)
        ->select();
        $btn_add = [
            'title' => '添加文案',
            'icon'  => 'fa fa-fw fa-plus-circle',
            'href'  => url("copywrite_add",['postcardId' => $postcardId])
        ];
        $btn_update = [
            'title' => '修改文案',
            'icon'  => 'fa fa-pencil fa-fw',
            'href'  => url("copywrite_update",['copywriteId' => "__copywriteId__",'postcardId' => $postcardId])
        ];
        return ZBuilder::make('table')
        ->setPageTitle('文案列表')
        ->setPrimaryKey('copywriteId')	//设置主键
        ->setTableName('copywrite')
        ->setSearch(['content' => '内容']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['content', '内容'],
           // ['type', '类型'],
            ['right_button', '操作', 'btn']
        ])
        
        ->setRowList($data) // 设置表格数据
        ->addTopButton('btn_add', $btn_add) // 添加顶部按钮
        ->addRightButtons('delete') // 批量添加右侧按钮
        ->addRightButton('btn_update', $btn_update) // 批量添加右侧按钮
        ->fetch();
    }
    
    
    public function copywrite_add($postcardId=null){
        if($postcardId==null){
            return $this->error("ID错误，请从列表中进入！");
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['createAt']=date("Y-m-d H:i:s");
            //halt($data);
            $r=db('copywrite')->insert($data);
            if($r){
                return $this->success('添加成功',url('/configs/postcard/copywrite_list/postcardId/'.$postcardId));
            }else {
                return $this->error('添加失败',url('/configs/postcard/copywrite_list/postcardId/'.$postcardId));
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
        ->setPageTitle('新增') // 设置页面标题
        ->addFormItems([ // 批量添加表单项
            ['hidden', 'postcardId',$postcardId],
            ['text', 'content', '内容'],
        ])
        //->isAjax(false)
        ->fetch();
    }
	
	
	public function copywrite_update($copywriteId=null,$postcardId=1){
	    if ($copywriteId === null) $this->error('缺少参数');
	    $info = db('copywrite')->find($copywriteId);
	    if ($this->request->isPost()) {
	        $data = $this->request->post();
	        $r=db('copywrite')->where("copywriteId",$copywriteId)->update($data);
	        if($r){
	            return $this->success('修改成功',url('/configs/postcard/copywrite_list/postcardId/'.$postcardId));
	        }else {
	            return $this->error('修改失败',url('/configs/postcard/copywrite_list/postcardId/'.$postcardId));
	        }
	    }
	    return ZBuilder::make('form')
	    ->setPageTitle('编辑') // 设置页面标题
	    ->addFormItems([ // 批量添加表单项
	        ['text', 'content', '内容'],
	    ])
	    ->setFormData($info) // 设置表单数据
	    ->fetch();
	    
	}
	
	
	
	
	
	//阅读人员列表
	public function sound_list($albumId){
	    if(empty($albumId)){
	        return $this->error("请选择评价");
	    }
	    $data = Db::name('sound')
	    ->alias("a")
	    ->join("user b","a.user_id=b.id","left")
	    
	    ->where("a.albumId",$albumId)
	    ->field("a.soundId,a.createAt,b.username")
	    ->select();
	    $btn_add = [
	        'title' => '添加声音',
	        'icon'  => 'fa fa-fw fa-plus-circle',
	        'href'  => url("sound_add",['albumId' => $albumId])
	    ];
	    $btn_update = [
	        'title' => '修改声音',
	        'icon'  => 'fa fa-pencil fa-fw',
	        'href'  => url("sound_update",['soundId' => "__soundId__",'albumId' => $albumId])
	    ];
	    return ZBuilder::make('table')
	    ->setPageTitle('阅读列表')
	    ->setPrimaryKey('soundId')	//设置主键
	    ->setTableName('sound')
	    //->setSearch(['content' => '内容']) // 设置搜索参数
	    ->addColumns([ // 批量添加列
	        ['username', '阅读人'],
	        ['createAt', '阅读时间'],
	        ['right_button', '操作', 'btn']
	    ])
	    
	    ->setRowList($data) // 设置表格数据
	    //->addTopButton('btn_add', $btn_add) // 添加顶部按钮
	    ->addRightButtons('delete') // 批量添加右侧按钮
	    //->addRightButton('btn_update', $btn_update) // 批量添加右侧按钮
	    ->fetch();
	}
	
	
	//评价列表
	public function appraise_list($albumId){
	    if(empty($albumId)){
	        return $this->error("请选择评价");
	    }
	    $data = Db::name('appraise')
	    ->alias("a")
	    ->join("user b","a.user_id=b.id","left")
	    
	    ->where("a.albumId",$albumId)
	    ->field("a.*,b.username")
	    ->select();
	    
	    return ZBuilder::make('table')
	    ->setPageTitle('评价列表')
	    ->setPrimaryKey('id')	//设置主键
	    ->setTableName('appraise')
	    ->addColumns([ // 批量添加列
	        ['username', '评价人'],
	        ['content', '评价内容'],
	        ['createAt', '评价时间'],
	        ['right_button', '操作', 'btn']
	    ])
	    
	    ->setRowList($data) // 设置表格数据
	    //->addTopButton('btn_add', $btn_add) // 添加顶部按钮
	    ->addRightButtons('delete') // 批量添加右侧按钮
	    //->addRightButton('btn_update', $btn_update) // 批量添加右侧按钮
	    ->fetch();
	}
	
	
	public function sound_add($albumId=null){
	    if($albumId==null){
	        return $this->error("ID错误，请从列表中进入！");
	    }
	    if ($this->request->isPost()) {
	        $data = $this->request->post();
	        $data['createAt']=date("Y-m-d H:i:s");
	        //halt($data);
	        $r=db('sound')->insert($data);
	        if($r){
	            return $this->success('添加成功');
	        }else {
	            return $this->error('添加失败');
	        }
	    }
	    // 使用ZBuilder快速创建表单
	    return ZBuilder::make('form')
	    ->setPageTitle('新增') // 设置页面标题
	    ->addFormItems([ // 批量添加表单项
	        ['hidden', 'albumId',$albumId],
	        ['text', 'title', '标题', ''],
	        ['number', 'duration', '播放时长(秒)'],
	        ['image', 'coverPath', '图片'],
	        ['file', 'url', '声音文件'],
	    ])
	    //->isAjax(false)
	    ->fetch();
	}
	
	
	public function sound_update($soundId=null,$albumId=1){
	    if ($soundId === null) $this->error('缺少参数');
	    $info = db('sound')->find($soundId);
	    if ($this->request->isPost()) {
	        $data = $this->request->post();
	        $r=db('sound')->where("soundId",$soundId)->update($data);
	        if($r){
	            return $this->success('修改成功');
	        }else {
	            return $this->error('修改失败');
	        }
	    }
	    return ZBuilder::make('form')
	    ->setPageTitle('编辑') // 设置页面标题
	    ->addFormItems([ // 批量添加表单项
	        ['text', 'title', '标题', ''],
	        ['number', 'duration', '播放时长(秒)'],
	        ['image', 'coverPath', '图片'],
	        ['file', 'url', '声音文件'],
	    ])
	    ->setFormData($info) // 设置表单数据
	    ->fetch();
	    
	}
}
?>